package common.controls.windows
{
	
	import common.controls.AKSprite;
	import common.controls.utils.DragController;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import kernel.application.ApplicationDisplay;
	
	/**  
	 * 
	 * @author carrey
	 * */ 
	public class AKWindow extends AKSprite
	{
		protected var _owner:DisplayObjectContainer;
		
		private var _validRect:Rectangle = null;
		
		private var _dragController:DragController = null;
		private var rectArea:Sprite;
		
		public function AKWindow( owner:DisplayObjectContainer )
		{
			this._owner = owner;
			super();
		}
		
		public function destroy():void
		{
			_validRect = null;
			_owner = null;
			if( _dragController != null )
			{
				_dragController.destroy();
				_dragController = null;
			}
		}
		
		protected function set dragArea( value:InteractiveObject ):void
		{
			if( _dragController != null )
			{
				_dragController.destroy();
			}
			if( value != null )
			{
				_dragController = new DragController( this, value );
			}
		}
		
		protected function set validRect( value:Rectangle ):void
		{
			_validRect = value;
		}
		
		protected function get validRect():Rectangle
		{
			if( _validRect==null )
			{
				_validRect = this.getBounds( this );
			}
			return _validRect;
		}
		
		
		public function setCenterLocation(isMask:Boolean=true):void
		{
			var rect:Rectangle = validRect;
			setLocation( (ApplicationDisplay.stage.stageWidth - rect.x - rect.width)/2+68, (ApplicationDisplay.stage.stageHeight - rect.y - rect.height)/2 -30);
			
			if(isMask)
			{
				rectArea = new Sprite();
				rectArea.graphics.beginFill( 0,0);
				rectArea.graphics.drawRect( 0, 0,960 , ApplicationDisplay.stage.stageHeight-30 );
			}
		}
		
		public function show():void
		{
			if( null==parent )
			{
				if(rectArea != null)
				{
					_owner.addChild(rectArea);
				}
				_owner.addChild( this );
			}
		}
		
		public function close():void
		{
			if( parent != null )
			{
				if(rectArea != null)
				{
					parent.removeChild(rectArea);
				}
				parent.removeChild( this );
			}
		}
	}
}